/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hvtt.web.controller;

import com.hvtt.web.connection.Command;
import com.hvtt.web.connection.WEBConnection;
import com.hvtt.web.connection.WEBParamCollection;
import com.hvtt.web.models.Si_nganhang;
import com.hvtt.web.models.Si_nganhang;
import com.hvtt.web.models.Si_nganhang;
import com.hvtt.web.models.Si_tinh;
import com.hvtt.web.utilities.Utilities;
import java.io.IOException;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.apache.commons.lang3.StringUtils;
import org.primefaces.context.RequestContext;

/**
 *
 * @author Administrator
 */

@ManagedBean
@SessionScoped
public class SI_NganHangController implements Serializable{
    private WEBConnection DAL=new WEBConnection();
    
    private Si_nganhang item= new Si_nganhang();

    /**
     * @return the item
     */
    public Si_nganhang getItem() {
        return item;
    }

    /**
     * @param item the item to set
     */
    public void setItem(Si_nganhang item) {
        this.item = item;
    }
    
    
    public List<Si_tinh> findAllTinh() throws SQLException
    {
        WEBParamCollection Params=new WEBParamCollection();
        Params.add("".equals(this.getItem().getQuocGiaID().trim())?"VN":this.getItem().getQuocGiaID());
        
        List<Si_tinh> lst= DAL.executeQuery("Select* from SI_Tinh where QuocGiaID=? order by ThuTu asc",Command.sqlText,Params,Si_tinh.class);
        return lst;        
    }
    
    
     public List<Si_nganhang> findAllNganHang() throws SQLException
    {
       
        
        List<Si_nganhang> lst= DAL.executeQuery("Select* from SI_NganHang",Si_nganhang.class);
        return lst;        
    }
    
     private String validateFields() throws SQLException {
        String str="";
        if (StringUtils.isBlank(this.getItem().getTenNH()))
        {
            str+="  + " + Utilities.getLanguages("TenNH")+": " + Utilities.getLanguages("Require") + "<br/>";
        }
        if (StringUtils.isBlank(this.getItem().getDiaChi()))
        {
            str+="  + " + Utilities.getLanguages("DiaChi")+": " + Utilities.getLanguages("Require") + "<br/>";
        }
        
        if("".equals(str.trim()))
            return "";
        return "<b style='color:black;'>" + Utilities.getLanguages("AdMsg0010") + ":</b> <br/>" +  "<b style='color:red;'>" + str + "</b>";
    }
    
    public void btnSave_Click(ActionEvent event) throws IOException, SQLException {
       
        this.getItem().setTinhID(String.valueOf(
                FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("TinhID")));
        this.getItem().setTenNH(String.valueOf(
             FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("TenNH")));
         this.getItem().setQuocGiaID(String.valueOf(
             FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("QuocGiaID")));
         this.getItem().setDiaChi(String.valueOf(
            FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("DiaChi")));
        this.getItem().setDienThoai(String.valueOf(
          FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("DienThoai")));
        
           this.getItem().setEmail(String.valueOf(
          FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("Email")));
              this.getItem().setGioiThieu(String.valueOf(
          FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("GioiThieu")));
          
           String result = this.validateFields();
      
            if (!result.isEmpty()) {
                FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Message Status",result);
                FacesContext.getCurrentInstance().addMessage(null, msg);
                RequestContext.getCurrentInstance().execute("update=\"growl\"");
                return;
            }
          try   
          {
            WEBParamCollection Params=new WEBParamCollection();
            Params.add("Save");
		Params.add(this.item.getID());
		Params.add(this.item.getTenNH());
		Params.add(this.item.getDiaChi());
		Params.add(this.item.getDienThoai());
		Params.add(this.item.getEmail());
		Params.add(this.item.getQuocGiaID());
		Params.add(this.item.getTinhID());
		Params.add(this.item.getCapCha());
		Params.add(this.item.getGioiThieu());
		Params.add(this.item.getTrangThai());
		
            DAL.executeUpdate("{call Proc_si_nganhang_In_Up_Del(?,?,?,?,?,?,?,?,?,?,?)}", Command.sqlStoredProcedure, Params);
            FacesContext.getCurrentInstance().getExternalContext().redirect("/Admin/si/banks.html");
            
            this.item.resetAttribute();
          }
          catch(Exception ex)
          {
              throw  ex;
          }
    }
    
     public void btnDelete_Click(ActionEvent event) throws IOException, SQLException {
       
        
          this.getItem().setID(Long.valueOf(
                FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("ID")));
         
          try   
          {
            WEBParamCollection Params=new WEBParamCollection();
            Params.add("Delete");
            Params.add(this.item.getID());
		Params.add(this.item.getTenNH());
		Params.add(this.item.getDiaChi());
		Params.add(this.item.getDienThoai());
		Params.add(this.item.getEmail());
		Params.add(this.item.getQuocGiaID());
		Params.add(this.item.getTinhID());
		Params.add(this.item.getCapCha());
		Params.add(this.item.getGioiThieu());
		Params.add(this.item.getTrangThai());
		
            DAL.executeUpdate("{call Proc_si_nganhang_In_Up_Del(?,?,?,?,?,?,?,?,?,?,?)}", Command.sqlStoredProcedure, Params);
            FacesContext.getCurrentInstance().getExternalContext().redirect("/Admin/si/banks.html");
          }
          catch(Exception ex)
          {
              throw  ex;
          }
    }
    public void btnUpdate_Click(ActionEvent event) throws IOException, SQLException {
       
        
          this.getItem().setID(Long.valueOf(
                FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("ID")));
         
          try   
          {
            WEBParamCollection Params=new WEBParamCollection();
            Params.add("GetByKey");
            Params.add(this.item.getID());
		Params.add(this.item.getTenNH());
		Params.add(this.item.getDiaChi());
		Params.add(this.item.getDienThoai());
		Params.add(this.item.getEmail());
		Params.add(this.item.getQuocGiaID());
		Params.add(this.item.getTinhID());
		Params.add(this.item.getCapCha());
		Params.add(this.item.getGioiThieu());
		Params.add(this.item.getTrangThai());
		
            
            List<Si_nganhang> lst= DAL.executeQuery("{call Proc_si_nganhang_In_Up_Del(?,?,?,?,?,?,?,?,?,?,?)}", Command.sqlStoredProcedure, Params,Si_nganhang.class);
            if(lst!=null && lst.size()>0)
            {
                this.setItem(lst.get(0));
                FacesContext.getCurrentInstance().getExternalContext().redirect("/Admin/si/banks/new.html");
            }
          }
          catch(Exception ex)
          {
              throw  ex;
          }
    }
    
    
    
    
    
    
    
    
    
    
    public void cmd_New_Click(ActionEvent event) throws IOException, SQLException {
        this.item.resetAttribute();
        
        FacesContext.getCurrentInstance().getExternalContext().redirect("/Admin/si/banks/new.html");
        RequestContext.getCurrentInstance().execute("update=\"frmdaily\"");
        
      }
    
    
    
     public void cmd_getListTinh_Click(ActionEvent event) throws IOException, SQLException {
        this.item.setQuocGiaID(String.valueOf(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("QuocGiaID").toString()));
       // RequestContext.getCurrentInstance().execute("update=\"pnDoiXe\"");
    }
}
